//
//  MTMemberViewController.swift
//  MT
//
//  Created by ooops on 2020/1/10.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

class MTMemberViewController: BaseViewController {
    
    //private var group: JMSGGroup?
    private var users: [JMSGUser]?
    private var isGroup: Bool = false
    lazy var flowLayout: UICollectionViewFlowLayout = {
        let flowLayout = UICollectionViewFlowLayout()
        let marginLR: CGFloat = 24
        let items: CGFloat = 5
        let itemWidth: CGFloat = 50
        let spacing: CGFloat = CGFloat((ScreenWidth - marginLR * 2 - items * itemWidth - 20) / (items - 1))
        flowLayout.scrollDirection = .vertical
        flowLayout.minimumInteritemSpacing = spacing
        flowLayout.minimumLineSpacing = 10
        //flowLayout.headerReferenceSize = CGSize(width: 40, height: 60)
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth + 20)
        return flowLayout
    }()
    
    lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView(frame: view.frame, collectionViewLayout: flowLayout)
        collectionView.bounces = true
        collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "kHeaderView")
        
        collectionView.backgroundColor = UIColor.theme.background
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(MTMemberViewCell.self, forCellWithReuseIdentifier: MTMemberViewCell.reuseIdentity())
        collectionView.contentInset = UIEdgeInsets(top: 30, left: 24, bottom: 30, right: 24)
        return collectionView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = self.isGroup ? "所有成员" : "所有活动成员"
        setupUI()
        makeConstraint()
    }
    
    private func setupUI() {
        view.addSubview(collectionView)
    }
    
    private func makeConstraint() {
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    

    convenience init(group: JMSGGroup) {
        self.init()
        self.isGroup = true
        group.memberInfoList({ (result, error) in
            if error == nil {
                if let userInfos: [JMSGGroupMemberInfo] = result as? [JMSGGroupMemberInfo] {
                    let r : [JMSGUser?] = userInfos.map { (info) -> JMSGUser? in
                        return info.user
                    }
                    
                    if let result = r as? [JMSGUser] {
                        self.users = result
                        DispatchQueue.main.async {
                            self.collectionView.reloadData()
                        }
                    }
                }
            }
        })
    }
    
    convenience init(users: [JMSGUser]?) {
        self.init()
        self.users = users
    }

}

extension MTMemberViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    
    //MARK: UICollectionViewDataSource
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // TODO:- Required Method
        return self.users?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MTMemberViewCell.reuseIdentity(), for: indexPath) as! MTMemberViewCell
        configureCell(cell: cell, forItemAt: indexPath)
        
        return cell
    }
    
    func configureCell(cell: MTMemberViewCell, forItemAt indexPath: IndexPath) {
        if let user = users?[indexPath.item] {
            cell.bindDate(user)
        }
    }
    
    //MARK: UICollectionViewDelegate
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let user = users?[indexPath.item] {
            let vc = MineViewController(username: user.username)
            navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        
    }
}
